package com.wxy.controller;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.wxy.entity.Userinfor;
import com.wxy.entity.Users;
import com.wxy.mapper.UserinforMapper;
import com.wxy.mapper.UsersMapper;
import com.wxy.selectUser.SelectUser;
import org.apache.tomcat.util.http.fileupload.UploadContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.MultipartConfigElement;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wxy
 * @since 2022-01-12
 */
@RestController
@RequestMapping("/userInfor")
public class UserinforController {

    @Autowired
    private UserinforMapper userinforMapper;

    @Autowired
    private UsersMapper usersMapper;

    String path = "/www/wwwroot/www.wkserver.top/userImg";

    //header获取名字
    @GetMapping("/getUserName")
    public Userinfor getName(HttpServletRequest request){
        String userId =  request.getHeader("userId");
        return userinforMapper.selectOne(SelectUser.userinforSelectUser(userId));
    }
    //获取注册时间
    @GetMapping("/getRegisterTime")
    public String getRegisterTime(
            HttpServletRequest request
    ){
        String userId =  request.getHeader("userId");
        return String.valueOf(userinforMapper.selectOne(SelectUser.userinforSelectUser(userId)).getRegisterTime());
    }

    //获取所在行业
    @GetMapping("/getIndustry")
    public String getIndustry(
            HttpServletRequest request
    ){
        String userId =  request.getHeader("userId");
        return String.valueOf(userinforMapper.selectOne(SelectUser.userinforSelectUser(userId)).getIndustry());
    }

    //上传照片
    @PostMapping("/uploadImg")
    public void uploadImg(
            HttpServletRequest request,
            @RequestParam("file") MultipartFile multipartFile
    ){
        //uuid改写文件名称
        String uuid = UUID.randomUUID().toString().replace("-","");
        //保存图片存储到服务器并使用唯一标识
        String newFileName = uuid;
        File file = new File(path,newFileName);
        //url存储到数据库
        QueryWrapper qw = SelectUser.userinforSelectUser(String.valueOf(request.getHeader("userId")));
        Userinfor userinfor = new Userinfor();
        userinfor.setImg("./userImg/"+uuid);
        userinforMapper.update(userinfor,qw);
        //保存图片
        try {
            multipartFile.transferTo(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    //修改名字
    @PostMapping("/changeUserName")
    public Boolean changeUserName(
            HttpServletRequest request,
            @RequestBody JSONObject jsonObject
            ){
        //   拿出ID
        String userId =  request.getHeader("userId");
        // 修改
        UpdateWrapper<Userinfor> uw= new UpdateWrapper<>();
        uw.eq("userId", Integer.valueOf(userId));
        Userinfor userinfor = new Userinfor();
        userinfor.setUserName(jsonObject.getString("userName"));
        userinforMapper.update(userinfor,uw);
        return true;
    }

    //修改登录密码
    @PostMapping("/updataPassword")
    public Boolean updataPassword(
            HttpServletRequest request,
            @RequestBody  JSONObject jsonObject
    ){
        String userId =  request.getHeader("userId");
        String olderPassWord =  jsonObject.getString("olderPassWord");
        String newPassWord =  jsonObject.getString("newPassWord");
        //查找到对象
        QueryWrapper qw = SelectUser.usersSelectUser(userId);
        String qwPassword = usersMapper.selectOne(qw).getPassWord();
        if(qwPassword.equals(olderPassWord)){
            Users users = new Users();
            users.setPassWord(newPassWord);
            usersMapper.update(users,qw);
            return true;
        }else {
            return false;
        }
    }

    //修改登录密码
    @PostMapping("/updateIndustry")
    public Boolean updateIndustry(
            HttpServletRequest request,
            @RequestBody JSONObject jsonObject
    ){
        String userId =  request.getHeader("userId");
        //修改对象
        Userinfor userinfor = new Userinfor();
        userinfor.setIndustry(jsonObject.getString("industry"));
        userinforMapper.update(userinfor,SelectUser.userinforSelectUser(userId));
        return true;
    }
}

